<?php

class Admin_Model_History extends Zend_Db_Table_Abstract
{
    protected $_name = 'history';
    public function getAllData(){
        $query = $this->select()
                        ->from($this->_name)
                        ->order('ord DESC')
                        ->order('catId ASC');
        return $this->fetchAll($query);
    }
    public function getSearchData($option){
        $query = $this->select()
                        ->from(array('h'=>$this->_name))
                        ->joinLeft(array('u'=>'user'),'h.userId = u.userId',array('username'))
                        ->setIntegrityCheck(false)
                        ->order('historyId DESC');
        if(isset($option['userId']) && is_numeric($option['userId']) && $option['userId']){
            $query->where('h.userId = ?', $option['userId']);
        }
        if(isset($option['mod']) && $option['mod']){
            $query->where('h.controller = ?', $option['mod']);
        }
        if(isset($option['gameS']) && is_array($option['gameS']) && $option['gameS']){
            $query->where('h.obj = \'game\' AND h.objField IN (?)',$option['gameS']);
        }
        if(isset($option['startTime']) && is_numeric($option['startTime']) && $option['startTime']){
            $query->where('h.time >= ?', $option['startTime']);
        }
        if(isset($option['endTime']) && is_numeric($option['endTime']) && $option['endTime']){
            $query->where('h.time <= ?', $option['endTime']);
        }
        if(isset($option['limit']) && $option['limit']){
            $limit = explode(',',$option['limit']);
            if(isset($limit[0]) && isset($limit[1]) && is_numeric($limit[0]) && is_numeric($limit[1])){
                $itemPerPage    = $limit[0];
                $offset         = $limit[1];
                $query->limit($itemPerPage,$offset);
            }
        }
        $result = $this->fetchAll($query);
        return $result;
    }
    public function countSearchData($option){
        $query = $this->select()
                        ->from(array('h'=>$this->_name),array('count(*) as Total'))
                        ->joinLeft(array('u'=>'user'),'h.userId = u.userId',array('username'))
                        ->setIntegrityCheck(false);
        if(isset($option['userId']) && is_numeric($option['userId']) && $option['userId']){
            $query->where('h.userId = ?', $option['userId']);
        }
        if(isset($option['mod']) && $option['mod']){
            $query->where('h.controller = ?', $option['mod']);
        }
        if(isset($option['gameS']) && is_array($option['gameS']) && $option['gameS']){
            $query->where('h.obj = \'game\' AND h.objField IN (?)',$option['gameS']);
        }
        if(isset($option['startTime']) && is_numeric($option['startTime']) && $option['startTime']){
            $query->where('h.time >= ?', $option['startTime']);
        }
        if(isset($option['endTime']) && is_numeric($option['endTime']) && $option['endTime']){
            $query->where('h.time <= ?', $option['endTime']);
        }
        $result = $this->fetchRow($query);
        return $result['Total'];
    }
    public function addData($data){
        if(is_array($data))
            return $this->insert($data);
        return false;
    }
}

























